package com.pi.pro.xcmg.am.was.web.cust.controller;

import com.alibaba.fastjson.JSONArray;
import com.pi.common.CodesConstants;
import com.pi.common.utils.StringUtils;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.DateUtils;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.csv.CsvExporter;
import com.pi.framework.web.util.CsvView;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.common.XcmgAmSupport;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.act.TaskActionDto;
import com.pi.wms.entity.TwaInventoryDetails;
import com.pi.wms.entity.TwaInventoryLotattr;
import com.pi.wms.entity.TwmBasMaterial;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@Slf4j
@Controller
@RequestMapping("/inventory/invdetails/ca")
public class CustApplyInventoryDetailsController extends BaseInventoryController<TwaInventoryDetails, Long> {

	@Override
	protected ICrudService<TwaInventoryDetails, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "invdetails";
	}

	@Override
	protected TwaInventoryDetails newModel(HttpServletRequest request) {
		return new TwaInventoryDetails();
	}

    @PostMapping("moveout")
    public String pickStation(@RequestParam("ids") Long[] ids, @RequestParam("pickstations") List<String> pickstations, Model model, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        //
        model.addAttribute("params", getParams(request));
        String m = "";

        TaskActionDto actionDto=new TaskActionDto();
        actionDto.setWorker(SecurityUtils.getLoginUsername());

        List<Long> storageIds=new ArrayList<>();
        m = this.wasCustomWorkService.moveout(storageIds, pickstations, actionDto);
        if (!StringUtils.isNullOrBlank(m)){
            addErrorMessage(redirectAttributes, m);
            return this.redirect("");
        }
        addSuccessMessage(redirectAttributes, "移库下架成功");
        return this.redirect("");
    }



	@PostMapping({"/export"})
	public ModelAndView export(@RequestParam final Map<String, Object> query, DataTablesInput input) {
		query.put("type", "SHIP");
		String inputParams = "";
		if(query.get("input") != null) {
			inputParams = query.get("input").toString();
		}
		if(StringUtils.isNotEmpty(inputParams)) {
			input = (DataTablesInput) JSONArray.parseObject(inputParams, DataTablesInput.class);
		}
		input.setLength(Integer.valueOf(-1));
		String ymd = DateUtils.format(new Date(), "yyyyMMddHHmmss");
		final DataTablesInput ins = input;
		CsvExporter<TwaInventoryDetails> exporter =  this.createExporter(query, ins, this.getMaterials(), this.getLotattrs());
		return new ModelAndView(new CsvView(exporter, "inventory_list_" + ymd));
	}


	protected String redirect(String relativeUrl) {
		if(!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
			relativeUrl = "/" + relativeUrl;
		}
		return "redirect:" + this.referUrl() + relativeUrl;
	}

	private String referUrl(){
		return "/inventory/invdetails/";
	}
}
